package com.piece.admin.controller.monitor;

import com.piece.admin.controller.ControllerSupport;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.redis.dto.RedisMonitorDTO;
import com.piece.core.redis.util.RedisInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;

@RestController
@Api(tags = {"Redis管理"})
@RequestMapping("/api/redis")
public class RedisController extends ControllerSupport {

    @GetMapping("/redis_info")
    @AclPermission(code = "monitor:server:view")
    @ApiOperation(value = "Redis服务器资源信息")
    public AjaxResponse redisInfo() {
        try {
            RedisMonitorDTO redisInfo = RedisInfo.getInstance().getRedisInfo();
            return success(I18nUtil.message(ExceptionConstants.MESSAGE_SUCCESS), redisInfo);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SERVER), e.getMessage());
        }
    }

    @GetMapping("/key_list")
    @AclPermission(code = "monitor:server:view")
    @ApiOperation(value = "查询Redis键列表")
    public AjaxResponse redisKeyList() {
        try {
            String match = getRequest().getParameter("match");
            Set<String> redisKeyList = RedisInfo.getInstance().getRedisKeyList(match);
            return success(I18nUtil.message(ExceptionConstants.MESSAGE_SUCCESS), redisKeyList);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SERVER), e.getMessage());
        }
    }

    @SubmitPermission
    @GetMapping("/key_value")
    @AclPermission(code = "monitor:server:view")
    @ApiOperation(value = "查询Redis键值")
    public AjaxResponse redisKeyValue() {
        try {
            String key = getRequest().getParameter("key");
            String value = RedisInfo.getInstance().getRedisKeyValue(key);
            return success(I18nUtil.message(ExceptionConstants.MESSAGE_SUCCESS), value);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SERVER), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "monitor:server:view")
    @ApiOperation(value = "删除Redis键值")
    public AjaxResponse redisDelete() {
        try {
            String key = getRequest().getParameter("key");
            String template = getRequest().getParameter("template");
            boolean result = false;
            if (StringUtil.isNotEmpty(key)) {
                result = RedisInfo.getInstance().delete(key);
            } else if (StringUtil.isNotEmpty(template)) {
                result = RedisInfo.getInstance().deleteTemplate(template);
            }

            if (result) {
                return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
            } else {
                return error(I18nUtil.message(ExceptionConstants.DELETE_FAILURE));
            }
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.DELETE_FAILURE), e.getMessage());
        }
    }
}
